package com.example.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.controller.utils.returnData;
import com.example.domain.Emp5;
import com.example.service.IEmp5Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/emp5s")
public class emp5Controller {

//    //创建该类记录日志的对象
//    private static final Logger log = LoggerFactory.getLogger(emp5Controller.class);


    @Autowired
    IEmp5Service service;

    @GetMapping
    public returnData getAll(){
        return new returnData(true,service.list());
    }

    @GetMapping("/{id}")
    public returnData getById(@PathVariable Integer id){
        return new returnData(true,service.getById(id));
    }

    @PostMapping
    public returnData save(@RequestBody Emp5 emp5){
        boolean b = service.save(emp5);
        return new returnData(b,b? "添加成功！":"添加失败！");  //可以用不写死的语言包，国际化
    }

    @DeleteMapping("/{id}")
    public returnData delById(@PathVariable Integer id){
        return new returnData(service.removeById(id));
    }

    @PutMapping
    public returnData updateById(@RequestBody Emp5 emp5){
        return new returnData(service.updateById(emp5));
    }

    @GetMapping("/{current}/{pageSize}")
    public returnData selectByPage(@PathVariable Integer current,@PathVariable Integer pageSize,Emp5 emp5){

        System.out.println("------------------------------------------------");

        IPage<Emp5> iPage=service.selectPage(current,pageSize,emp5);
        if (current>iPage.getPages()){
            //当前页码》总页码
            //再次查询
            iPage=service.selectPage((int)iPage.getPages(),pageSize,emp5);
        }
        return new returnData(true,iPage);

    }
}
